package com.chuanc.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanc.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Angel on 2017/10/18.
 */

public class MainFragmentAdapter extends  RecyclerView.Adapter<MainFragmentAdapter.ItemHolder> implements View.OnClickListener{

    private List<MainFragmentItem> itemList=new ArrayList();
    private Context mContext;

    static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        protected TextView itemTitle, count;
        protected ImageView image;

        public ItemHolder(View view) {
            super(view);
            this.image = (ImageView) view.findViewById(R.id.fragment_main_item_img);
            this.itemTitle = (TextView) view.findViewById(R.id.fragment_main_item_title);
            this.count = (TextView) view.findViewById(R.id.fragment_main_item_count);
            view.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {

        }
    }

    public MainFragmentAdapter(List<MainFragmentItem> itemList) {
        this.itemList=itemList;
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mContext==null){
            mContext=parent.getContext();
        }
        View view= LayoutInflater.from(mContext).inflate(R.layout.fragment_main_item,parent,false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder itemHolder, int position) {
        MainFragmentItem item = itemList.get(position);
        itemHolder.itemTitle.setText(item.title);
        itemHolder.count.setText("(" + item.count + ")");
        itemHolder.image.setImageResource(item.avatar);
        setOnListener(itemHolder);
    }

    private void setOnListener(ItemHolder itemHolder) {
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    @Override
    public void onClick(View v) {

    }

}
